package com.fanfanwang.ffw.app.ui.home;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.fanfanwang.ffw.app.AppConstant;
import com.fanfanwang.ffw.app.AppManager;
import com.fanfanwang.ffw.app.R;
import com.fanfanwang.ffw.app.adapter.SectionListAdapter;
import com.fanfanwang.ffw.app.asynctask.GetparkingListAsyncTask;
import com.fanfanwang.ffw.app.bean.ParkingListBean;
import com.fanfanwang.ffw.app.widget.CommonTitleBar;

/** 路线界面 */
public class SectionAct extends com.fanfanwang.ffw.app.base.BaseActivity {

	private CommonTitleBar commonTitleBar;
	private SectionListAdapter sectionListAdapter;
	private ArrayList<ParkingListBean> sectionLists;
	private ListView listview;
	private Activity mAct;
	private Intent mIntent;
	public static IGetSectionList iGetSectionList;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initWidget();
		initData();
		AppManager.getAppManager().addActivity(this);
		setListener();
	}

	public void initWidget() {
		setContentView(R.layout.activity_service_zhihang);
		mAct = this;
		commonTitleBar = (CommonTitleBar) findViewById(R.id.common_titlebar);
		listview = (ListView) findViewById(R.id.zhihang_listView);
		sectionLists = new ArrayList<ParkingListBean>();

	}

	public void widgetClick() {

	}

	@Override
	public void initData() {
		commonTitleBar.setCenterText(getString(R.string.section_list));
		commonTitleBar.setReturnVisible();
		// 设置得到数据的回调方法
		setSectionList();
		sectionListAdapter = new SectionListAdapter(mAct, sectionLists);
		listview.setAdapter(sectionListAdapter);
		// new GetSectionListAsyncTask().execute("");
		// 石家庄的是12
		// new GetParkListAsyncTask(mAct, "");
		new GetparkingListAsyncTask(mAct, AppConstant.NOWLONGITUDE + "",
				AppConstant.NOWLATITUDE + "",
				getString(R.string.default_distance)).execute("");
	}

	@Override
	protected void initView() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void setListener() {
		// TODO Auto-generated method stub
		commonTitleBar.setToolBarReturnOnclickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
					long arg3) {
				// TODO Auto-generated method stub
				mIntent = new Intent(mAct, SectionDetailAct.class);
				mIntent.putExtra("id", sectionLists.get(pos).id);
				mIntent.putExtra("longitude1", sectionLists.get(pos).longitude);
				mIntent.putExtra("latitude1", sectionLists.get(pos).latitude);
				startActivity(mIntent);

			}
		});
	}

	public interface IGetSectionList {
		public void setSectionList(ArrayList<ParkingListBean> beans);
	}

	public void setSectionList() {
		iGetSectionList = new IGetSectionList() {
			@Override
			public void setSectionList(ArrayList<ParkingListBean> beans) {
				sectionLists.clear();
				sectionLists.addAll(beans);
				sectionListAdapter.resetData(beans);
				sectionListAdapter.notifyDataSetChanged();
			}
		};
	}
}
